package com.zhuhjay.system.factory.entity;

import com.zhuhjay.common.utils.ClientUtils;
import com.zhuhjay.common.utils.ServletUtils;
import com.zhuhjay.system.domain.entity.LoginLog;

import java.util.Date;

/**
 * LoginLog实体类的构建工厂
 * @author ZhuHJay
 * @date 2022/3/30 20:11
 */
public class LoginLogBuilderFactory {

    private final LoginLog loginLog;

    public LoginLogBuilderFactory(){
        loginLog = new LoginLog();
    }

    /**
     * 登录用户
     */
    public LoginLogBuilderFactory username(String username){
        loginLog.setUsername(username);
        return this;
    }

    /**
     * 登录ip 及 地理位置
     */
    public LoginLogBuilderFactory loginIpAndLocation(String loginIp){
        loginLog.setLoginIp(loginIp);
        loginLog.setLoginLocation(ClientUtils.getCityInfo(loginIp));
        return this;
    }

    /**
     * 登录状态: 0失败 1成功
     */
    public LoginLogBuilderFactory status(Integer status){
        loginLog.setStatus(status);
        return this;
    }

    /**
     * 登录响应的数据
     */
    public LoginLogBuilderFactory message(String message){
        loginLog.setMsg(message);
        return this;
    }

    /** 日志对象构建 **/
    public LoginLog build(){
        /* 构造其余需要且不用手动记录的 */
        loginLog.setBrowser(ServletUtils.getBorderName());
        loginLog.setOs(ServletUtils.getOs());
        loginLog.setLoginTime(new Date());
        return loginLog;
    }

}
